package com.shan.mybatis.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shan.mybatis.mapper.EmpMapper;
import com.shan.mybatis.pojo.Emp;
import com.shan.mybatis.pojo.PageBean;
import com.shan.mybatis.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.JavaBean;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper mapper;

    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Long count = mapper.count();
//        List<Emp> page1 = mapper.page((page-1)*pageSize, pageSize);
//        PageBean pageBean = new PageBean(count, page1);
//        return pageBean;
//    }

    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> list = mapper.list();
        Page<Emp> p = (Page<Emp>) list;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public PageBean query(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> emps = mapper.selectLike(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) emps;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        mapper.deleteIds(ids);
    }

    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        mapper.addEmp(emp);
    }

    @Override
    public Emp queryById(Integer id) {
        Emp emp = mapper.selectById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        mapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return mapper.getByUsernmaeAndPassword(emp);
    }


}
